<!-- 大底订购查询：蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '0px 10px 5px' }">
        <div slot="header" class="f16">
          <div style="font-weight: 600; margin-top: 10px">大底订购明细</div>
        </div>
        <Table
          :tableHeader="tableHeader"
          :tableData="tableData"
          class="mt10"
          :isLoading="isLoading"
          :summaryData="summaryData"
          summary
          @selectList="selectList1"
        />
      </el-card>
    </div>
    <el-card shadow="never" :body-style="{ padding: '0px 5px 5px' }">
      <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent, onMounted, ref, watch } from 'vue'
import fileRequest from '@/utils/fileRequest'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'OutsoleOrdert'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import Find from '@/components/Find/index.vue'
import { selectList, selectSomeList } from '@/plugins/mixin'
import LoginWork from '@/utils/LoginWork'
import service from '@/utils/request'
import { ElMessage } from 'element-plus'
import { debounce } from 'lodash'
import Api from '@/api/GeneralManager/inquire/OrderSummary'
const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入指令号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入型体',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '品牌',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商代码 厂商名称',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '请输入订购单号',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '订购日期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
])
const tableHeader = ref([
  {
    label: '类型',
    prop: 'type',
    width: '70px',
    change: true
  },
  {
    label: '指令',
    prop: 'lotNo',
    width: '150px',
    change: true
  },
  {
    label: '货号',
    prop: 'style',
    width: '70px',
    change: true
  },
  {
    label: '品牌',
    prop: 'brand',
    width: '70px',
    change: true
  },
  {
    label: '颜色',
    prop: 'color',
    width: '100px',
    change: true
  },
  {
    label: '底模号',
    prop: 'moldNo',
    width: '100px',
    change: true
  },
  {
    label: '订单数',
    prop: 'orderQty',
    width: '50px',
    change: true
  },
  {
    label: '号码',
    prop: 'sizeNo',
    width: '50px',
    change: true
  },
  {
    label: '订购单号',
    prop: 'purNo',
    width: '140px',
    change: true
  },
  {
    label: '订购日期',
    prop: 'purDate',
    width: '75px',
    change: true
  },
  {
    label: '请购数',
    prop: 'purQty',
    width: '70px'
  },
  {
    label: '物料编码',
    prop: 'mtlNo',
    width: '100px',
    change: true
  },
  {
    label: '大底名称和颜色(双击调用信息到输入栏)',
    prop: 'mtlName',
    width: '330px',
    change: true
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    width: '50px',
    change: true
  },
  {
    label: '厂商',
    prop: 'vendorName',
    width: '70px',
    change: true
  },
  {
    label: '交期',
    prop: 'deliveryDate',
    width: '80px',
    change: true
  },
  {
    label: '备注',
    prop: 'remark',
    width: '120px',
    change: true
  },
  {
    label: '责任人',
    prop: 'dutyUser',
    width: '70px',
    change: true
  },
  {
    label: '责任原因',
    prop: 'dutyRek',
    width: '80px',
    change: true
  }
])

// 表一模块
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '导出') {
    const res = (await fileRequest.post(`/pcOrder/downloadMtlPurList`, {
      choose: '1',
      lotNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令号'
      )?.inputValue,
      style: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '型体')
        ?.selectValue,
      moldNo: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
        ?.selectValue,
      vendorName: formList.value.find(
        (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称'
      )?.selectValue,
      purNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入订购单号'
      )?.inputValue,
      purEndDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '订购日期'
      )?.dateValue?.[1],
      purStartDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '订购日期'
      )?.dateValue?.[0],
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}

const selectedList1 = ref([])
const selectList1 = (list: any) => {
  selectedList1.value = list
}
const tableData = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 6,
    num: 0
  },
  {
    index: 10,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}

// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/pcOrder/mtlPurList', {
    choose: '1',
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令号'
    )?.inputValue,
    style: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '型体')
      ?.selectValue,
    moldNo: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
      ?.selectValue,
    vendorName: formList.value.find(
      (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称'
    )?.selectValue,
    purNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入订购单号'
    )?.inputValue,
    purEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '订购日期'
    )?.dateValue?.[1],
    purStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '订购日期'
    )?.dateValue?.[0],
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.list
  total.value = tableDataList.total
  summaryData.value[0].num = tableDataList.qtyTotal
  summaryData.value[1].num = tableDataList.purQtyTotal
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)

// 责任原因,责任人 临时保存变量
const dutyReks = ref([])
const dutyUseres = ref([])
const getSelectList = async () => {
  await selectList(formList, Api.getSelectList)
  let vendorName = await service.get<any, any>('/back/vendorQuery')
  let { dutyRek, dutyUsers } = await service.get<any, any>('/back/duty/getDutyCondition')
  dutyReks.value = dutyRek
  dutyUseres.value = dutyUsers
  // let { molds } = await service.get<any, any>('/back/order/allOrderCondition')
  let vendorNames = formList.value.find(
    (item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称'
  ) as any
  // let mold = formList.value.find((item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '底模号') as any
  // // 插入底模
  // if (molds && mold) {
  //   (molds as string[]).forEach((item: string, index) => {
  //     if (item === null) return
  //     mold.selectOptions?.push({
  //       optionKey: index,
  //       optionValue: item,
  //       optionLabel: item
  //     })
  //   })
  // }
  // 插入厂商代码 厂商名称
  if (vendorName && vendorNames) {
    vendorName.forEach((item: any, index: any) => {
      if (item === null) return
      vendorNames.selectOptions?.push({
        optionKey: index,
        optionValue: `${item.vendorNo} ${item.vendorName}`
      })
    })
  }
}
onMounted(() => {
  getSelectList()
})
</script>

<style lang="less" scoped>
:deep(.el-form-item:nth-child(4)) {
  .el-select {
    width: 200px;
    height: 28px;
  }
}
</style>
